package org.yzh.client;

import io.github.yezhihao.protostar.annotation.Message;
import org.yzh.client.netty.ClientConfig;
import org.yzh.client.netty.HandlerMapping;
import org.yzh.client.netty.TCPClient;
import org.yzh.client.protocol.JT808Beans;
import org.yzh.protocol.basics.JTMessage;
import org.yzh.protocol.codec.JTMessageDecoder;
import org.yzh.protocol.codec.JTMessageEncoder;
import org.yzh.web.commons.RandomUtils;
import org.yzh.web.component.adapter.JTMessageAdapter;

import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 不依赖spring，快速启动netty服务
 */
public class ClientTest {

    private static final Scanner scanner = new Scanner(System.in);

    private static TCPClient tcpClient;

    public static void initClient(String ip, int port, String deviceId, String plateNo, String mobileNo) {
        JTMessageAdapter messageAdapter = new JTMessageAdapter(
            new JTMessageEncoder("org.yzh.protocol"),
            new JTMessageDecoder("org.yzh.protocol")
        );

        ClientConfig jtConfig = new ClientConfig.Builder()
            .setIp(ip)
            .setPort(port)
            .setMaxFrameLength(1024)
            .setDelimiters(new byte[]{0x7e})
            .setDecoder(messageAdapter)
            .setEncoder(messageAdapter)
            .setHandlerMapping(new HandlerMapping("org.yzh.client"))
            .setDeviceId(deviceId)
            .setPlateNo(plateNo)
            .setMobileNo(mobileNo)
            .build();

        tcpClient = new TCPClient(jtConfig).start();
    }

    public static void main(String[] args) {
//        String deviceId = "2021001";
//        String plateNo = "苏A12345";
        String deviceId = "MBLY";
        if (args.length > 0) {
            deviceId = args[0];
        }
        String plateNo = "苏A10000";
        if (args.length > 1) {
            plateNo = args[1];
        }
        String mobileNo = "15322334563";
        if (args.length > 2) {
            mobileNo = args[2];
        }
        String ip = "127.0.0.1";
        if (args.length > 3) {
            ip = args[3];
        }
        int port = 7611;
        if (args.length > 4) {
            port = Integer.parseInt(args[4]);
        }
        long interval = 100000;
        if (args.length > 5) {
            interval = Long.parseLong(args[5]);
        }
        /* 初始化客户端 */
        initClient(ip, port, deviceId, plateNo, mobileNo);

        /* 启动定时心跳 */
        String finalMobileNo = mobileNo;
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                JTMessage message = JT808Beans.T0002();
                message.setHeader(JT808Beans.H2013Header(message.getClass().getAnnotation(Message.class), finalMobileNo));
                tcpClient.writeObject(message);
            }
        },1000, 30000);

        while (true) {
//            System.out.println("选择发送的消息：1.注册 2.位置信息上报(所有附加信息) 3.FCW 4.UFCW 5.PCW 6.LDW 7.HMW 8.SLI 9.摄像头遮挡 10.位置信息上报(所有报警信息)");
//            int i = scanner.nextInt();
            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            int i = RandomUtils.nextInt(3, 10);
            JTMessage message = null;
            switch (i) {
                case 0:
                    tcpClient.stop();
                    return;
                case 1:
                    message = JT808Beans.T0100(deviceId, plateNo);
                    break;
                case 2:
                    message = JT808Beans.T0200Attributes();
                    break;
                case 3:
                    message = JT808Beans.T0200JSATL12(deviceId, 1 << 29, 0x01);
                    System.out.println("deviceId: " + deviceId + " send FCW msg");
                    break;
                case 4:
                    message = JT808Beans.T0200JSATL12(deviceId, 1 << 29, 0x08);
                    System.out.println("deviceId: " + deviceId + " send UFCW msg");
                    break;
                case 5:
                    message = JT808Beans.T0200JSATL12(deviceId, 1 << 29, 0x04);
                    System.out.println("deviceId: " + deviceId + " send PFCW msg");
                    break;
                case 6:
                    message = JT808Beans.T0200JSATL12(deviceId, 1 << 23, 0x02);
                    System.out.println("deviceId: " + deviceId + " send LDW msg");
                    break;
                case 7:
                    message = JT808Beans.T0200JSATL12(deviceId, 1 << 29, 0x03);
                    System.out.println("deviceId: " + deviceId + " send HMW msg");
                    break;
                case 8:
                    message = JT808Beans.T0200_SLI();
                    System.out.println("deviceId: " + deviceId + " send SLI msg");
                    break;
                case 9:
                    message = JT808Beans.T0200_OC();
                    System.out.println("deviceId: " + deviceId + " send 摄像头遮挡 msg");
                    break;
                default:
                    break;
            }
            if (message == null) {
                System.out.println("没有要选择的消息");
                continue;
            }
            message.setHeader(JT808Beans.H2013Header(message.getClass().getAnnotation(Message.class), mobileNo));
            tcpClient.writeObject(message);
        }
    }
}